package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.anno.GuiguCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@GuiguCache(
			cacheKey = RedisConstant.USER_INFO_PREFIX + "#{#params[0]}",
			enableLock = true ,
			lockName = RedisConstant.USER_INFO_LOCK_PREFIX + "#{#params[0]}"

	)
	@GetMapping(value = "/getUserInfoById/{userId}")
	public Result<UserInfo> getUserInfoById(@PathVariable(value = "userId") Long userId) {
		UserInfo userInfo = userInfoService.getById(userId);
		return Result.build(userInfo , ResultCodeEnum.SUCCESS) ;
	}

	/**
	 * 默认情况下通过openFegin发送远程调用会丢失自定义请求头。
	 * 该问题的解决方案？
	 * @param albumId
	 * @param trackIds
	 * @return
	 */
	@GuiGuLogin
	@PostMapping(value = "/userIsPaidTrack/{albumId}")
	public Result<Map<Long  , Integer>> userIsPaidTrack(@PathVariable(value = "albumId") Long albumId , @RequestBody List<Long> trackIds) {
		Map<Long  , Integer> map = userInfoService.userIsPaidTrack(albumId , trackIds) ;
		return Result.build(map , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	@GetMapping(value = "/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable(value = "albumId") Long albumId) {
		Boolean result = userInfoService.isPaidAlbum(albumId) ;
		return Result.build(result , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	@GetMapping(value = "/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable(value = "albumId") Long albumId) {
		List<Long> ids = userInfoService.findUserPaidTrackList(albumId) ;
		return Result.build(ids , ResultCodeEnum.SUCCESS) ;
	}

	// 更新vip的失效状态
	@GetMapping(value = "/updateVipExpireStatus")
	public Result updateVipExpireStatus() {
		log.info("UserInfoApiController...updateVipExpireStatus...执行了");
		userInfoService.updateVipExpireStatus() ;
		return Result.build(null , ResultCodeEnum.SUCCESS) ;
	}
}

